clear
set more off
program drop _all

use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta" ,clear
gen censored_gr =.
replace censored_gr = 1 if censored=="not censored"
replace censored_gr = 3 if censored=="left censored"
replace censored_gr = 2 if censored=="right censored"
replace censored_gr = 4 if censored=="left censored & right censored"
tab quarter censored_gr

bys product: egen revenue_r_sum=sum(revenue_r)
bys product: egen quality0_m=mean(quality0)
keep product censored_gr censored revenue_r_sum revenue_r_m quality0_m maxAge
duplicates drop
duplicates drop product, force  

gen total_prods=1
gen total_prods4=1 if maxAge<=4
gen total_prods16=1 if maxAge<=16
gen total_prods16p=1 if maxAge>16
gen total_prods28p=1 if maxAge>28
egen x=sum(revenue_r_sum)
egen y=sum(total_prods)
gen total_prods_w=revenue_r_sum/x
gen total_prods_s=total_prods/y
collapse (sum) total_prods total_prods_s total_prods_w revenue_r_sum total_prods4 total_prods16 total_prods16p total_prods28p ///
(mean) revenue_r_m_mean=revenue_r_m quality0_m_mean=quality0_m maxAge (median) revenue_r_m_median=revenue_r_m maxAge_median=maxAge quality0_m_median=quality0_m (p25) revenue_r_m_p25=revenue_r_m maxAge_p25=maxAge quality0_m_p25=quality0_m (p75) revenue_r_m_p75=revenue_r_m maxAge_p75=maxAge quality0_m_p75=quality0_m ///
(p90) revenue_r_m_p90=revenue_r_m maxAge_p90=maxAge quality0_m_p90=quality0_m (p95) revenue_r_m_p95=revenue_r_m maxAge_p95=maxAge quality0_m_p95=quality0_m, by (censored_gr)

replace maxAge = round(maxAge+1, 0.1)
replace  total_prods4= round(total_prods4/total_prods*100, 0.1)
replace  total_prods16= round(total_prods16/total_prods*100, 0.1)
replace  total_prods16p= round(total_prods16p/total_prods*100, 0.1)
replace  total_prods28p= round(total_prods28p/total_prods*100, 0.1)

replace  revenue_r_m_mean=round(revenue_r_m_mean/1000,0.1)
replace  revenue_r_m_p25= round(revenue_r_m_p25/1000, 0.1)
replace  revenue_r_m_median= round(revenue_r_m_median/1000, 0.1)
replace  revenue_r_m_p75= round(revenue_r_m_p75/1000, 0.1)
replace  revenue_r_m_p90= round(revenue_r_m_p90/1000, 0.1)
replace  revenue_r_m_p95= round(revenue_r_m_p95/1000, 0.1)

label define censored_gr_l 1 "Not Censored" 2 "Right Censored" 3 "Left Censored" 4 "Right & Left Censored"
label values censored_gr censored_gr_l
label variable total_prods "Total \# of products"
label variable total_prods_s "Share"
label variable total_prods_w "Share (rev. weighted)"
label variable maxAge "   average"
label variable total_prods4 "   less than 4"
label variable total_prods16 "   less than 16"
label variable total_prods28p "   above 28"
label variable revenue_r_m_mean "mean"
label variable revenue_r_m_p25 "25th percentile"
label variable revenue_r_m_median "median"
label variable revenue_r_m_p75 "75th percentile"
label variable revenue_r_m_p90 "90th percentile"
label variable revenue_r_m_p95 "95th percentile"
label variable quality0_m_mean "mean"
label variable quality0_m_p25 "25th percentile"
label variable quality0_m_median "median"
label variable quality0_m_p75 "75th percentile"
label variable quality0_m_p90 "90th percentile"
label variable quality0_m_p95 "95th percentile"

preserve
estpost tabstat total_prods maxAge total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 quality0_m_mean quality0_m_p25 quality0_m_median quality0_m_p75 quality0_m_p90 quality0_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo censored_v2
restore

estpost tabstat total_prods maxAge total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo censored_v1

use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta", clear
gen censored_gr =5
tab quarter censored_gr

bys product: egen revenue_r_sum=sum(revenue_r)
bys product: egen quality0_m=mean(quality0)
keep product censored_gr censored revenue_r_sum revenue_r_m quality0_m maxAge
duplicates drop
duplicates drop product, force   

gen total_prods=1
gen total_prods4=1 if maxAge<=4
gen total_prods16=1 if maxAge<=16
gen total_prods16p=1 if maxAge>16
gen total_prods28p=1 if maxAge>28
egen x=sum(revenue_r_sum)
egen y=sum(total_prods)
gen total_prods_w=revenue_r_sum/x
gen total_prods_s=total_prods/y
collapse (sum) total_prods total_prods_s total_prods_w revenue_r_sum total_prods4 total_prods16 total_prods16p total_prods28p ///
(mean) revenue_r_m_mean=revenue_r_m quality0_m_mean=quality0_m maxAge (median) revenue_r_m_median=revenue_r_m maxAge_median=maxAge quality0_m_median=quality0_m (p25) revenue_r_m_p25=revenue_r_m maxAge_p25=maxAge quality0_m_p25=quality0_m (p75) revenue_r_m_p75=revenue_r_m maxAge_p75=maxAge quality0_m_p75=quality0_m ///
(p90) revenue_r_m_p90=revenue_r_m maxAge_p90=maxAge quality0_m_p90=quality0_m (p95) revenue_r_m_p95=revenue_r_m maxAge_p95=maxAge quality0_m_p95=quality0_m, by (censored_gr)

replace maxAge = round(maxAge+1, 0.1)
replace  total_prods4= round(total_prods4/total_prods*100, 0.1)
replace  total_prods16= round(total_prods16/total_prods*100, 0.1)
replace  total_prods16p= round(total_prods16p/total_prods*100, 0.1)
replace  total_prods28p= round(total_prods28p/total_prods*100, 0.1)

replace  revenue_r_m_mean=round(revenue_r_m_mean/1000,0.1)
replace  revenue_r_m_p25= round(revenue_r_m_p25/1000, 0.1)
replace  revenue_r_m_median= round(revenue_r_m_median/1000, 0.1)
replace  revenue_r_m_p75= round(revenue_r_m_p75/1000, 0.1)
replace  revenue_r_m_p90= round(revenue_r_m_p90/1000, 0.1)
replace  revenue_r_m_p95= round(revenue_r_m_p95/1000, 0.1)

label define censored_gr_l 5 "All"
label values censored_gr censored_gr_l
label variable total_prods "Total \# of products"
label variable total_prods_s "Share"
label variable total_prods_w "Share (rev. weighted)"
label variable maxAge "   average"
label variable total_prods4 "   less than 4"
label variable total_prods16 "   less than 16"
label variable total_prods28p "   above 28"
label variable revenue_r_m_mean "mean"
label variable revenue_r_m_p25 "25th percentile"
label variable revenue_r_m_median "median"
label variable revenue_r_m_p75 "75th percentile"
label variable revenue_r_m_p90 "90th percentile"
label variable revenue_r_m_p95 "95th percentile"
label variable quality0_m_mean "mean"
label variable quality0_m_p25 "25th percentile"
label variable quality0_m_median "median"
label variable quality0_m_p75 "75th percentile"
label variable quality0_m_p90 "90th percentile"
label variable quality0_m_p95 "95th percentile"

preserve
estpost tabstat total_prods maxAge total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 quality0_m_mean quality0_m_p25 quality0_m_median quality0_m_p75 quality0_m_p90 quality0_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo all_v2
restore

estpost tabstat total_prods maxAge total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo all_v1

esttab all_v1 censored_v1 using "OUTPUT/Table1.tex", cell(mean(fmt(%9.0fc %9.1gc))) collabels(none) refcat(maxAge "\\Duration (quarters)" revenue_r_m_mean "\\Revenue (quarterly, \\$1,000)" products_r_m_mean "\\Products (quarterly)", nolabel) nostar nonotes unstack nonumbers noobs not label replace


